﻿function KssHelper() {
    this.groupBy = function (array, predicate) {
        var grouped = {};
        for (var i = 0; i < array.length; i++) {
            var groupKey = predicate(array[i]);
            if (typeof (grouped[groupKey]) === "undefined")
                grouped[groupKey] = [];
            grouped[groupKey].push(array[i]);
        }

        return grouped;
    };
    this.statusDisplay = function (status) {
        switch (status) {
            case 0:
                return "รอหัวหน้างานอนุมัติ";
            case 10:
                return "รอแผนกอนุมัติ";
            case 11:
                return "เอกสารไม่ผ่านการอนุมัติ";
            case 20:
                return "รอ TQM อนุมัติ";
            case 30:
                return "เอกสารผ่านการอนุมัติ";
            case 40:
                return "เอกสารผ่านการอนุมัติ";
        }
    }
    this.GetlastDoc = function () {
        AysPost("getlastDoc",
            _documentData,
            func);
    };
    this.InserForm = function (_documentData, func) {
        AysPost("SaveForm",
            _documentData,
            func);
    };
    this.GetDocByID = function (docId, func) {
       
        AysPost("getdocbyID",
              { DocID: docId },
            func);
    };
    this.GetDocByID = function (Empid, func) {

        AysPost("getemp",
              { empid: Empid },
            func);
    };
    this.PayCashs = function (_documentData, func) {
        AysPost("PayCashs",
            _documentData,
            func);
    };
    this.GetDocByUserName = function (userName, func) {

        AysPost("getdocbyUsername",
              { UserName: userName },
            func);
    };
    this.AddHrUser = function (hrUser, func) {
        AysPost("addHrUser",
             hrUser,
           func);
    }
    this.UpdateHrUser = function (hrUser, func) {
        AysPost("updateHrUser",
             hrUser,
           func);
    }
    this.ExportCSV = function(getDataUrl) {
        
        var uri = getDataUrl;
        window.open(uri);
    }
    this.ResetPassword = function (newPassword) {
        AysPost("resetPassword",
                     newPassword,
                   func);
    }
}

this.AysPost = function (_methodName, _postData, func, async) {
    //waitingDialog({ title: "Loading...", message: "Please wait." });
    var domain = "/Api.ashx";
    var Async = false;
        if (async == false) {
            Async = false;
        }
    var _data = { metdhodName: _methodName, postData: _postData };
    var uriPost = domain;
    jQuery.ajax({
        type: "POST",
        url: uriPost,
        async: Async,
        data: _data,
        dataType: "json",
        success: function (data) {
            if (func) {
                if (data && data.Error === '-0') {
                    window.location = '/Page/login.aspx';
                }
                func(data);
            }
            //if (_methodName != 'GetCurrentUserDetail') {
            //closeWaitingDialog();
            //}
        },
        error: function () {
            //closeWaitingDialog();
        }
    });
};
function formatDate(val) {
    if (val === null || val === '') {
        return null;
    }
    else {
        var d = new Date(parseInt(val.substr(6)));
        return jQuery.datepicker.formatDate('dd-MM-yy', d) + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();
    }
}